package com.tanhua.server.controller;

import com.tanhua.server.service.MovementsService;
import com.tanhua.server.service.QuanziMQService;
import com.tanhua.server.utils.UserThreadLocal;
import com.tanhua.server.vo.Movements;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.VisitorsVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("movements")
@Slf4j
public class MovementsController {

    @Autowired
    private MovementsService movementsService;

    @Autowired
    private QuanziMQService quanziMQService;


    /**
     * 发送动态
     *
     * @param textContent   文本文件
     * @param location      地址
     * @param longitude     经度
     * @param latitude      维度
     * @param multipartFile 传入图片的多文件
     * @return ResponseEntity页面响应
     */
    @PostMapping()
    public ResponseEntity<Void> savePublish(@RequestParam(value = "textContent", required = false) String textContent,
                                            @RequestParam(value = "location", required = false) String location,
                                            @RequestParam("longitude") String longitude,
                                            @RequestParam("latitude") String latitude,
                                            @RequestParam(value = "imageContent", required = false) MultipartFile[] multipartFile
                                            ) {
        try {
            //1.调用业务层movementsService,调用保存发布信息的方法,返回的是boolean值,传入的是从页面传入的参数
            String publishId = this.movementsService.saveMovements(textContent, location, longitude, latitude, multipartFile);

            //2.若返回的值为true,说明保存信息成功,向页面响应ok()
            if (StringUtils.isNotEmpty(publishId)) {
                /**
                 * 发送消息
                 */
                this.quanziMQService.publishMsg(publishId);
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            //3.失败日志信息
            log.error("保存发布信息失败~" + e);
        }

        //3.1失败后向页面响应错误信息
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }



    /**
     * 查询好友动态
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping
    public PageResult queryPublishList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                       @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        return this.movementsService.queryPublishList(UserThreadLocal.get(),page, pageSize);
    }

    /**
     * 查询推荐动态
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("recommend")
    public ResponseEntity<PageResult> queryRecommendPublishList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                                @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize){
        try {
            PageResult pageResult = this.movementsService.queryPublishList(UserThreadLocal.get(),page, pageSize);

            return ResponseEntity.ok(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    /**
     * 点赞
     * @param publishId
     * @return
     */
    @GetMapping("/{id}/like")
    public ResponseEntity<Long> likeComment(@PathVariable(value = "id") String publishId){
        try {
            Long count = this.movementsService.likeComment(publishId);

            if (count != null){
                /**
                 * 发送点赞消息
                 */
                this.quanziMQService.likePublishMsg(publishId);
                return ResponseEntity.ok(count);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 取消点赞
     * @param publishId
     * @return
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity<Long> disLikeComment(@PathVariable(value = "id") String publishId){
        try {
            Long disLikeCount = this.movementsService.disLikeComment(publishId);

            if (disLikeCount != null){
                /**
                 * 发送取消点赞消息
                 */
                this.quanziMQService.disLikePublishMsg(publishId);

                return ResponseEntity.ok(disLikeCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 喜欢
     * @param publishId
     * @return
     */
    @GetMapping("/{id}/love")
    public ResponseEntity<Long> loveComment(@PathVariable(value = "id") String publishId){
        try {
            Long loveCount = this.movementsService.loveComment(publishId);

            if (loveCount != null){
                /**
                 * 发送喜欢消息
                 */
                this.quanziMQService.lovePublishMsg(publishId);

                return ResponseEntity.ok(loveCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 取消点赞
     * @param publishId
     * @return
     */
    @GetMapping("/{id}/unlove")
    public ResponseEntity<Long> unLoveComment(@PathVariable(value = "id") String publishId){
        try {
            Long unLoveCount = this.movementsService.unLoveComment(publishId);

            if (unLoveCount != null){
                /**
                 * 发送取消喜欢消息
                 */
                this.quanziMQService.disLovePublishMsg(publishId);

                return ResponseEntity.ok(unLoveCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
    /**
     * 查询单条动态信息
     * @param publishId
     * @return
     */
    @GetMapping("{id}")
    public ResponseEntity<Movements> queryMovementsById(@PathVariable(value = "id") String publishId){
        try {
            Movements movements = this.movementsService.queryMovementsById(publishId);

            if(null != movements){
                /**
                 * 发送消息
                 */
                this.quanziMQService.queryPublishMsg(publishId);

                return ResponseEntity.ok(movements);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 谁看过我
     * @return
     */
    @GetMapping("visitors")
    public ResponseEntity<List<VisitorsVo>> queryVisitorsList(){
        try {
            //1.查询访客列表
            List<VisitorsVo> list =  this.movementsService.queryVisitorsList();

            //2.响应给页面
            return ResponseEntity.ok(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 自己的所有动态
     *
     * @return
     */
    @GetMapping("all")
    public ResponseEntity<PageResult> queryAlbumList(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                                     @RequestParam(value = "pagesize",defaultValue = "10") Integer pageSize,
                                                     @RequestParam(value = "userId") Long userId){
        try {
            PageResult pageResult = this.movementsService.queryAlbumList(userId,page,pageSize);

            return ResponseEntity.ok(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();

    }

}